<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        :multiple-selected="false"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :width="column.width" :label="column.title" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100px" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button v-if="row.status !== 'done'" type="text" title="执行" size="mini" icon="el-icon-document-add" @click="execute(row.id)" />
            <!-- <el-button type="text" title="查看" size="mini" icon="el-icon-view" @click="view(row.id)" /> -->
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <el-dialog v-if="dialogFormVisible" custom-class="second-dialog-height" title="执行" :append-to-body="true" class="select-dialog" :visible.sync="dialogFormVisible" width="90%">
      <EditForm :id="editId" ref="editForm" />
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click=" cancel()">取 消</el-button>
        <el-button size="mini" type="warning" @click="ok('doing')">保存草稿</el-button>
        <el-button size="mini" type="primary" @click="ok('done')">提交</el-button>
      </span>
    </el-dialog>
    <ViewDialog v-if="dialogViewVisible" custom-class="second-dialog-height" title="查看" :append-to-body="true" :show.sync="dialogViewVisible" width="90%" @on-cancel="viewCancel">
      <ViewForm :id="editId" />
    </ViewDialog>
    <ViewDialog v-if="dialogViewRuleVisible" custom-class="second-dialog-height" title="维护说明" :append-to-body="true" :show.sync="dialogViewRuleVisible" width="60%" @on-cancel="viewRuleCancel">
      <ViewRuleForm :id="ruleId" ref="ViewRuleForm" :task-id="taskId" />
    </ViewDialog>
  </div>
</template>

<script>
import ViewDialog from '@neu/biz-core/components/ViewDialog'
import ViewRuleForm from './components/viewRuleForm'
import ViewForm from './components/viewForm'
// import FormDialog from '@neu/biz-core/components/FormDialog'
import TableFilter from './components/tableFilter'
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'
import {
  periodPage as page,
  remove
} from '@neu/dcom-ui/api/maintainTask'

export default {
  name: 'MaintainTaskPage',
  components: {
    TableFilter,
    //  FormDialog,
    EditForm,
    ActionBar,
    PageableTable,
    ViewDialog,
    ViewRuleForm,
    ViewForm
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        {
          title: '任务编码',
          key: 'code',
          render: (h, params) => { // 查看功能
            return h('span', [
              h('el-link', {
                class: 'font-12',
                attrs: { title: params.row.code },
                props: { type: 'primary' },
                on: { click: () => { this.view(params.row.id) } }},
              params.row.code)
            ])
          }
        },
        // {
        //   title: '任务名称',
        //  width: 150,
        //   key: 'name'
        // },
        // {
        //   title: '计划编码',
        // width: 150,
        //   key: 'planCode'
        // },
        {
          title: '计划名称',
          key: 'planName'
        },
        // {
        //   title: '计划版本',
        //   key: 'version'
        // },
        {
          title: '维护类别',
          key: 'modelName'
        },
        {
          title: '维护对象',
          key: 'objName'
        },
        {
          title: '维护部门',
          key: 'orgName'
        },
        {
          title: '维护规则',
          key: 'ruleName',
          render: (h, params) => { // 查看功能
            return h('span', [
              h('el-link', {
                class: 'font-12',
                attrs: { title: params.row.ruleName },
                props: { type: 'primary' },
                on: { click: () => { this.viewRule(params.row.ruleId, params.row.id) } }},
              params.row.ruleName)
            ])
          }
        },
        {
          title: '执行人',
          key: 'chargeUserName'
        },
        {
          title: '任务状态',
          key: 'status',
          render: (h, params) => {
            var text = ''
            if (params.row.status === 'undo') {
              text = '未执行'
            }
            if (params.row.status === 'doing') {
              text = '执行中'
            }
            if (params.row.status === 'done') {
              text = '已执行'
            }
            return h('span', [
              h('el-tag', text)
            ])
          }
        },
        {
          title: '是否停止服务',
          key: 'suspendService',
          render: (h, params) => {
            const text = params.row.suspendService === true ? '是' : '否'
            return h('span', [
              h('el-tag', text)
            ])
          }
        },
        // {
        //   title: '量化时间(小时)',
        //   key: 'quantifyingTime'
        // },
        {
          title: '开始时间',
          key: 'startDateTime',
          sortable: 'custom',
          render: (h, params) => { return h('span', formatDate(params.row.startDateTime)) }
        },
        {
          title: '结束时间',
          key: 'endDateTime',
          sortable: 'custom',
          render: (h, params) => { return h('span', formatDate(params.row.endDateTime)) }
        },
        {
          title: '创建时间',
          key: 'createTime',
          sortable: 'custom',
          render: (h, params) => { return h('span', formatDate(params.row.createTime)) }
        }
      ],
      dialogFormVisible: false,
      dialogViewVisible: false,
      dialogViewRuleVisible: false,
      editId: -1,
      ruleId: -1,
      taskId: -1
    }
  },
  methods: {
    view(id) {
      // 打开对话框
      this.editId = id
      this.dialogViewVisible = true
    },
    execute(id) {
      // 打开对话框
      this.editId = id
      this.dialogFormVisible = true
    },
    // 对话框确认事件处理
    ok(value) {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save(value).then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    },
    viewCancel() {
      this.editId = -1
      // 关闭对话框
      this.dialogViewVisible = false
    },
    viewRule(ruleId, id) {
      // 打开对话框
      this.ruleId = ruleId
      this.taskId = id

      if (this.taskId === -1 || this.ruleId === -1 || this.ruleId === null || this.ruleId === 0) {
        return this.$message({
          showClose: true,
          message: '当前任务没有匹配维护规则',
          type: 'warning'
        })
      }

      this.dialogViewRuleVisible = true
    },
    viewRuleCancel() {
      this.ruleId = -1
      this.taskId = -1
      // 关闭对话框
      this.dialogViewRuleVisible = false
    }
  }
}
</script>
